package com.simple.service.fee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.simple.po.FeeReport;
import com.xunj.core.CoreDao;
import com.xunj.po.SysCodeDict;
import com.xunj.util.Common;

public class FeeWrittenOffService {
	
	private CoreDao dao;

	public FeeWrittenOffService(CoreDao dao) {
		this.dao = dao;
	}

	
	/**
	 * 更新费用汇总上报值
	 * @param filePath
	 * @throws Exception
	 */
	public void totalFeeReport(String feeReportId) throws Exception
	{
		FeeReport feeReport = (FeeReport) dao.findOne(FeeReport.class, feeReportId);
		String sumHql = "select new Map(sum(f.billAmount) as billAmount," +
		"sum(f.certificateAmount) as certificateAmount," +
		"sum(f.billCount) as billCount," +
		"sum(f.certificateCount) as certificateCount," +
		"sum(fw.billAmountWrittenOff) as billAmountWrittenOff," +
		"sum(fw.certificateAmountWrittenOff) as certificateAmountWrittenOff," +
		"sum(fw.billCountWrittenOff) as billCountWrittenOff," +
		"sum(fw.certificateCountWrittenOff) as certificateCountWrittenOff" +
		") from FeeDetail f,FeeWrittenOff fw where f.feeId=fw.feeId and f.feeReportId='"+feeReportId+"'";

		List feeTotalList = dao.findAll(sumHql);
		if(!feeTotalList.isEmpty())
		{
			Map map = (Map) feeTotalList.get(0);
			feeReport.setReportBillAmount((Double)map.get("billAmount"));
			if(map.get("billCount")!=null)
				feeReport.setReportBillCount(Integer.parseInt(map.get("billCount")+""));
			feeReport.setReportCertificateAmount((Double)map.get("certificateAmount"));
			if(map.get("certificateCount")!=null)
				feeReport.setReportCertificateCount(Integer.parseInt(map.get("certificateCount")+""));
			feeReport.setBillAmountTol((Double)map.get("billAmountWrittenOff"));
			if(map.get("billCountWrittenOff")!=null)
				feeReport.setBillCountTol(Integer.parseInt(map.get("billCountWrittenOff")+""));
			feeReport.setCertificateAmountTol((Double)map.get("certificateAmountWrittenOff"));
			if(map.get("certificateCountWrittenOff")!=null)
				feeReport.setCertificateCountTol(Integer.parseInt(map.get("certificateCountWrittenOff")+""));
		}
		dao.update(feeReport);
	}
	/**
	 * 生成核销汇总表
	 * @param writtenOffList	核销列表数据
	 * @param addLink	是否添加链接
	 * @param showSbje	是否显示申报金额
	 * @return
	 * @throws Exception
	 */
	public Map createWrittenOffTolData(List writtenOffList,boolean addLink,boolean showSbje) throws Exception
	{
		Map dataMap = new HashMap();
		
		ServletContext ctx = ServletActionContext.getServletContext();
		FeeService fs = new FeeService(dao);
		
		//查询组织费用类别数据，当前费用中包含的费用类别
		List codeList = (List) ctx.getAttribute("CodeDict");

		List currentCodeList = new ArrayList();
		for(int i=0;i<codeList.size();i++)
		{
			SysCodeDict cd = (SysCodeDict) codeList.get(i);
			for(int j=0;j<writtenOffList.size();j++)
			{
				Map fMap = (Map) writtenOffList.get(j);
				if(((String)fMap.get("feeTypeId")).indexOf(cd.getCodeId())==0)
				{
					currentCodeList.add(cd);
					break;
				}
			}
		}
		List newCodeList = fs.getFeeTypeList(currentCodeList, "004");
		fs.disposalFeeTypeList(newCodeList);
		
		String tdTitle1 = "";
		String tdTitle2 = "";
		String tdTitle3 = "";
		String tdTitle4 = "";
		//标题跨行数
		int titleColspan=2;
		int baseColSpan = 1;
		if(showSbje)
			baseColSpan = 2;
		for(int i=0;i<newCodeList.size();i++)
		{
			Map codeMap1 = (Map) newCodeList.get(i);
			int level1Colspan=0;
			//二级
			List childList2 = (List) codeMap1.get("childList");
			for(int j=0;j<childList2.size();j++)
			{
				Map codeMap2 = (Map) childList2.get(j);
				//三级
				List childList3 = (List) codeMap2.get("childList");
				String codeName2 = "";
				//原费用项目，非组织标题时补充或汇总统计列
				if(codeMap2.get("sign").equals("o")||codeMap2.get("sign").equals("t"))
					codeName2 = (String) codeMap2.get("codeName");
				tdTitle2+="<td class='feeTitle' colspan='"+childList3.size()*baseColSpan+"'>"+codeName2+"</td>";
				level1Colspan+=childList3.size();
				for(int k=0;k<childList3.size();k++)
				{
					Map codeMap3 = (Map) childList3.get(k);
					String codeName3 = "";
					if(codeMap3.get("sign").equals("o"))
						codeName3 = (String) codeMap3.get("codeName");
					tdTitle3+="<td colspan='"+baseColSpan+"' class='feeTitle'>"+codeName3+"</td>";
					if(showSbje)
						tdTitle4+="<td class='feeTitle' width='60'>申报</td>";
					tdTitle4+="<td class='feeTitle' width='60'>核销</td>";
					if(showSbje)
						titleColspan+=2;
					else
						titleColspan+=1;
				}
			}
			tdTitle1+="<td class='feeTitle' colspan='"+level1Colspan*baseColSpan+"'>"+codeMap1.get("codeName")+"</td>";
		}
		//把各类费用合并到一行
		List feeTargetList = new ArrayList();
		//三个合计,一个总计Map
		Map totalDatasMap = new HashMap();
		totalDatasMap.put("地区人员", new HashMap());
		totalDatasMap.put("地区办事处", new HashMap());
		totalDatasMap.put("销售分线", new HashMap());
		Map tolMap_all = new HashMap();
		tolMap_all.put("targetName","总计");
		//核销数额各项总计
		Double billAmountTol = 0d;
		Double certificateAmountTol = 0d;
		Integer billCountTol = 0;
		Integer certificateCountTol = 0;
		//统计数据，将同一个对象的数据放在一行
		for(int i=0;i<writtenOffList.size();i++)
		{
			Map fMap = (Map) writtenOffList.get(i);

			Map feeMap = null;
			for(int j=0;j<feeTargetList.size();j++)
			{
				Map map = (Map) feeTargetList.get(j);
				if(map.get("targetId")!=null&&map.get("targetId").equals(fMap.get("targetId")))
					feeMap = map;
			}
			if(feeMap==null)
			{
				feeMap = new HashMap();
				feeMap.put("targetId", fMap.get("targetId"));
				feeMap.put("targetName", fMap.get("targetName"));
				feeMap.put("areaId", fMap.get("areaId"));
				feeMap.put("areaName", fMap.get("areaName"));
				feeMap.put("areaDeptId", fMap.get("areaDeptId"));
				feeMap.put("areaDeptName", fMap.get("areaDeptName"));
				feeMap.put("feeTarget", fMap.get("feeTarget"));
				feeMap.put("feeReportId", fMap.get("feeReportId"));
				feeTargetList.add(feeMap);
			}
			String feeTypeId = (String) fMap.get("feeTypeId");
			//头层费用类别编码
			String feeTypeIdL1 = feeTypeId.substring(0,6);
			String feeTypeIdLAll = feeTypeId.substring(0,3);
			feeMap.put("targetId_"+feeTypeId, fMap.get("targetId"));
			//申报
			feeMap.put("sbje_"+feeTypeId, Common.sum((Double)feeMap.get("sbje_"+fMap.get("feeTypeId")),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			//核销
			feeMap.put("hxje_"+feeTypeId, Common.sum((Double)feeMap.get("hxje_"+fMap.get("feeTypeId")),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			//留审
			feeMap.put("lsje_"+feeTypeId, Common.sum((Double)feeMap.get("lsje_"+fMap.get("feeTypeId")),(Double)fMap.get("billAmountLeaveReview"),(Double)fMap.get("certificateAmountLeaveReview")));
			//申报小计
			feeMap.put("sbjexj_"+feeTypeIdL1, Common.sum((Double)feeMap.get("sbjexj_"+feeTypeIdL1),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			//核销小计
			feeMap.put("hxjexj_"+feeTypeIdL1, Common.sum((Double)feeMap.get("hxjexj_"+feeTypeIdL1),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			//申报列总结
			feeMap.put("sbjezj_"+feeTypeIdLAll, Common.sum((Double)feeMap.get("sbjezj_"+feeTypeIdLAll),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			//核销列总结
			feeMap.put("hxjezj_"+feeTypeIdLAll, Common.sum((Double)feeMap.get("hxjezj_"+feeTypeIdLAll),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			
			Map tolMap = (Map) totalDatasMap.get(fMap.get("feeTarget"));
			tolMap.put("targetName", fMap.get("feeTarget")+"合计");
			tolMap.put("sbje_"+feeTypeId, Common.sum((Double)tolMap.get("sbje_"+feeTypeId),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			tolMap.put("hxje_"+feeTypeId, Common.sum((Double)tolMap.get("hxje_"+feeTypeId),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			tolMap.put("sbjexj_"+feeTypeIdL1, Common.sum((Double)tolMap.get("sbjexj_"+feeTypeIdL1),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			tolMap.put("hxjexj_"+feeTypeIdL1, Common.sum((Double)tolMap.get("hxjexj_"+feeTypeIdL1),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			tolMap.put("sbjezj_"+feeTypeIdLAll, Common.sum((Double)tolMap.get("sbjezj_"+feeTypeIdLAll),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			tolMap.put("hxjezj_"+feeTypeIdLAll, Common.sum((Double)tolMap.get("hxjezj_"+feeTypeIdLAll),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			//总计
			tolMap_all.put("sbje_"+feeTypeId, Common.sum((Double)tolMap_all.get("sbje_"+feeTypeId),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			tolMap_all.put("hxje_"+feeTypeId, Common.sum((Double)tolMap_all.get("hxje_"+feeTypeId),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			tolMap_all.put("sbjexj_"+feeTypeIdL1, Common.sum((Double)tolMap_all.get("sbjexj_"+feeTypeIdL1),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			tolMap_all.put("hxjexj_"+feeTypeIdL1, Common.sum((Double)tolMap_all.get("hxjexj_"+feeTypeIdL1),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			tolMap_all.put("sbjezj_"+feeTypeIdLAll, Common.sum((Double)tolMap_all.get("sbjezj_"+feeTypeIdLAll),(Double)fMap.get("billAmount"),(Double)fMap.get("certificateAmount")));
			tolMap_all.put("hxjezj_"+feeTypeIdLAll, Common.sum((Double)tolMap_all.get("hxjezj_"+feeTypeIdLAll),(Double)fMap.get("billAmountWrittenOff"),(Double)fMap.get("certificateAmountWrittenOff")));
			//核销数额总计
			billAmountTol = Common.sum(billAmountTol,(Double)fMap.get("billAmountWrittenOff"));
			certificateAmountTol = Common.sum(certificateAmountTol,(Double)fMap.get("certificateAmountWrittenOff"));
			billCountTol = Common.sum(billCountTol,(Integer)fMap.get("billCountWrittenOff"));
			certificateCountTol = Common.sum(certificateCountTol,(Integer)fMap.get("certificateCountWrittenOff"));
		
			
			//根据数据分类情况将类别小计放在对应明细汇总之下
			//下一个数据对象
			Map nextfMap = null;
			if(i!=writtenOffList.size()-1)
			{
				nextfMap = (Map) writtenOffList.get(i+1);
			}
			if(nextfMap!=null)
			{
				if(!fMap.get("feeTarget").equals(nextfMap.get("feeTarget")))
					feeTargetList.add(totalDatasMap.get(fMap.get("feeTarget")));
			}
			else
				feeTargetList.add(totalDatasMap.get(fMap.get("feeTarget")));
		}


		feeTargetList.add(tolMap_all);
		StringBuffer listHtml = new StringBuffer();
		//当前迭代的费用对象标识
		for(int f=0;f<feeTargetList.size();f++)
		{
			//合计列的td样式
			String totalStyle = "";
			Map feeMap = (Map) feeTargetList.get(f);
			if(feeMap.get("targetId")==null)
				totalStyle = " class='feeTol'";
			listHtml.append("<tr><td colspan='2'"+totalStyle+">"+feeMap.get("targetName")+"</td>");
			for(int i=0;i<newCodeList.size();i++)
			{
				Map codeMap1 = (Map) newCodeList.get(i);
				//二级
				List childList2 = (List) codeMap1.get("childList");
				for(int j=0;j<childList2.size();j++)
				{
					Map codeMap2 = (Map) childList2.get(j);
					//三级
					List childList3 = (List) codeMap2.get("childList");

					for(int k=0;k<childList3.size();k++)
					{
						Map codeMap3 = (Map) childList3.get(k);
						String getKeySuffex = "";
						//统计取小计列
						if(codeMap3.get("sign").equals("t"))
						{
							getKeySuffex="xj";
						}else if(codeMap3.get("sign").equals("z"))
						{
							getKeySuffex="zj";
						}
						//如果显示申报金额
						if(showSbje)
							listHtml.append("<td>"+Common.convertNull(feeMap.get("sbje"+getKeySuffex+"_"+codeMap3.get("codeId")),"")+"</td>");
						if(feeMap.get("targetId_"+codeMap3.get("codeId"))!=null&&addLink==true)
						{
							//留审金额
							double lsje = (Double) feeMap.get("lsje"+getKeySuffex+"_"+codeMap3.get("codeId"));
							String lsStyle = "";
							if(lsje!=0)
								lsStyle = "style='color:red'";
							listHtml.append("<td><a href='#' "+lsStyle+" onclick=\"writtenOffData('"+feeMap.get("targetId_"+codeMap3.get("codeId"))+"','"+codeMap3.get("codeId")+"')\">"+Common.convertNull(feeMap.get("hxje_"+codeMap3.get("codeId")),"")+"</a></td>");
						}
						else
							listHtml.append("<td>"+Common.convertNull(feeMap.get("hxje"+getKeySuffex+"_"+codeMap3.get("codeId")),"")+"</td>");
						
					}
				}
			}
			listHtml.append("</tr>");
			
			
		}
		//计算的核销合计值，核销时使用
		dataMap.put("billAmountTol",billAmountTol);
		dataMap.put("billCountTol",billCountTol);
		dataMap.put("certificateAmountTol",certificateAmountTol);
		dataMap.put("certificateCountTol",certificateCountTol);
		
		dataMap.put("titleColspan",titleColspan);
		dataMap.put("tdTitle1", tdTitle1);
		dataMap.put("tdTitle2", tdTitle2);
		dataMap.put("tdTitle3", tdTitle3);
		dataMap.put("tdTitle4", tdTitle4);
		dataMap.put("listHtml", listHtml.toString());
		
		return dataMap;
	}
	
}
